<template>
	<view class="search-container">
		<view class="search-wrap">
			<uni-search-bar @input="inputSearch" :radius="100" cancelButton="none" @confirm="search" :focus="true" />
		</view>
		<!-- 搜索历史 -->
		<view class="search-history" v-if="searchList.length === 0">
			<view class="history-title">
				<view class="history-name">搜索历史</view>
				<uni-icons type="trash" size="18" @click="clear"></uni-icons>
			</view>
			<view class="history-list">
				<view class="history-list-item" v-for="(item, index) in computedSearchHistoryList" :key="index">{{item}}
				</view>
			</view>
		</view>

		<!-- 搜索列表 -->
		<view class="search-list" v-else>
			<view class="search-list-item" v-for="item in searchList" :key="item.goods_id" @click="goGoodsList(item)">
				<view class="text">{{item.goods_name}}</view>
				<uni-icons type="right" size="17"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				searchText: '',
				// 搜索结果列表
				searchList: [],
				// 搜索历史
				searchHistoryList: []
			};
		},
		onLoad() {
			this.searchHistoryList = JSON.parse(uni.getStorageSync('searchHistoryList')) || []
		},
		methods: {
			// 获取搜索结果
			async getSearchList() {
				if (!this.searchText.trim()) {
					this.searchList = []
					return
				}
				// 发起请求，获取搜索建议列表
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.searchText
				})
				if (res.meta.status !== 200) return uni.$showMsg()

				this.searchList = res.message
			},
			inputSearch(value) {
				// 清除定时器
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {

					this.searchText = value
					this.getSearchList()
					if (!this.searchText.trim()) return
					// 将Array数组转化成Set对象
					const setArr = new Set(this.searchHistoryList)
					// 2. 调用 Set 对象的 delete 方法，移除对应的元素
					setArr.delete(this.searchText)
					// 3. 调用 Set 对象的 add 方法，向 Set 中添加元素
					setArr.add(this.searchText)
					// 4. 将 Set 对象转化为 Array 数组
					this.searchHistoryList = Array.from(setArr)
					uni.setStorageSync('searchHistoryList', JSON.stringify(this.searchHistoryList))
				}, 500)
			},
			// 清除搜索历史记录
			clear() {
				this.searchHistoryList = []
				uni.removeStorageSync('searchHistoryList')
			},
			// 
			goGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + item.goods_id
				})
			}
		},
		computed: {
			computedSearchHistoryList() {
				// 注意：由于数组是引用类型，所以不要直接基于原数组调用 reverse 方法，以免修改原数组中元素的顺序
				// 而是应该新建一个内存无关的数组，再进行 reverse 反转
				return [...this.searchHistoryList].reverse()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-wrap {
		position: sticky;
		top: 0;
		z-index: 9999;
		height: 50px;
		background-color: #C00000;
	}

	.search-list {
		padding: 0 5px;
		background-color: #FFFFFF;

		.search-list-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;

			.text {
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.search-history {
		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
			padding: 0 5px;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.history-list-item {
				min-width: 15px;
				font-size: 13px;
				text-align: center;
				margin: 5px;
				padding: 5px 10px;
				background-color: #efefef;
				border: 1px solid #efefef;
				border-radius: 3px;
			}
		}
	}
</style>
